import 'package:baselib/utils/image_util.dart';
import 'package:baselib/utils/px_base_util.dart';
import 'package:flutter/material.dart';
import 'package:framelib/utils/color_util.dart';
import 'package:framelib/utils/px_util.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CommMessageDialog extends Dialog {
  String? imgPath;
  double? imgWidth;
  String message;
  TextStyle? messageTextStyle;
  String? negativeText;
  String? positiveText;
  TextStyle? negativeTextStyle;
  TextStyle? positiveTextStyle;
  Function? onCloseEvent;
  Function? onPositivePressEvent;
  bool? isTouchOutSideDissmiss;
  bool? isTouchKeyBackDissmiss;


  CommMessageDialog({
    super.key,
    required this.message,
    this.imgPath,
    this.imgWidth,
    this.messageTextStyle,
    this.negativeTextStyle,
    this.positiveTextStyle,
    this.negativeText,
    this.positiveText,
    this.onPositivePressEvent,
    this.isTouchOutSideDissmiss=true,
    this.isTouchKeyBackDissmiss=true,
    this.onCloseEvent,
  });

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: (){
        if(isTouchKeyBackDissmiss??false){
          Navigator.of(context).pop();
        }
        return Future(() => false);
      },
      child: GestureDetector(
        onTap: (){
          if(isTouchOutSideDissmiss??false){
            Navigator.of(context).pop();
          }
        },
        child: Material(
          type: MaterialType.transparency,
          child:
          Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              GestureDetector(
                  onTap: (){

                  },
                  child:new Container(
                    decoration: ShapeDecoration(
                      color: color_white,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.all(
                          Radius.circular(px_10.sp),
                        ),
                      ),
                    ),
                    constraints: BoxConstraints(
                      // minHeight: ScreenUtil.getInstance().getAdapterSize(PxUtils.px_65),
                        maxWidth: MediaQuery.sizeOf(context).width*3/4
                    ),
                    margin:  EdgeInsets.only(
                        bottom: pxStatusBarHeight(context: context)
                    ),
                    child:
                    new Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        (imgPath??'').isEmpty?SizedBox(width: px_0,):
                        Container(
                          margin: EdgeInsets.only(
                            top: px_base_30.h
                          ),
                          child: ImageUtil.loadAssetImage(
                              imgPath??'',
                              width: px_50.w,
                              fit: BoxFit.fitWidth
                          ),
                        ),
                        // (imgPath??'').isEmpty?SizedBox(width: px_0,):
                        // SizedBox(height: px_14,),
                        Container(
                          alignment: Alignment.center,
                          constraints: BoxConstraints(minHeight: px_66.h),
                          padding: EdgeInsets.fromLTRB(px_15.w, (imgPath??'').isEmpty?px_base_30.h:px_15.h,
                              px_15.w, px_20.h),
                          child: new Text(
                            message??'',
                            style: messageTextStyle??TextStyle(fontSize: px_17.sp,color: Color(0xff455262)),
                          ),
                        ),

                        new Container(
                          color:  Color(0xffe0e0e0),
                          height: px_0d5.h,
                        ),
                        Container(
                          child: _buildBottomButtonGroup(context),
                        )
                      ],
                    ),
                  ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildBottomButtonGroup(BuildContext context) {
    List<Widget> widgets = <Widget>[];
    if ((negativeText??'').isNotEmpty) widgets.add(_buildBottomCancelButton(context));
    widgets.add(new Container(
      color: Color(0xffe0e0e0),
        width: px_0d5.w,
        height: px_20.h,
    ));
    if (positiveText != null && positiveText!.isNotEmpty) widgets.add(_buildBottomPositiveButton(context));
    return new Flex(
      direction: Axis.horizontal,
      children: widgets,
    );
  }

  Widget _buildBottomCancelButton(BuildContext context) {
    return Flexible(
      fit: FlexFit.tight,
      child: GestureDetector(
        onTap: onCloseEvent==null?(){
          Navigator.of(context).pop();
        }:(()=>onCloseEvent!()),
        child: Container(
          color: Colors.transparent,
          width: double.infinity,
          padding: EdgeInsets.only(
            top: px_12.h,
            bottom: px_12.h,
          ),
          alignment: Alignment.center,
          child: new Text(
            negativeText??'',
            textAlign: TextAlign.center,
            style: negativeTextStyle??TextStyle(
              fontSize: px_16.sp,
              color: Color(0xff888888)
            ),
          ),
        ),
      ),
    );
  }

  Widget _buildBottomPositiveButton(BuildContext context) {
    return new Flexible(
      fit: FlexFit.tight,
      child: GestureDetector(
        onTap: (){
          Navigator.of(context).pop();
          if(onPositivePressEvent!=null)onPositivePressEvent!();
        },
        child: Container(
          color: Colors.transparent,
          width: double.infinity,
          padding: EdgeInsets.only(
            top: px_12.h,
            bottom: px_12.h,
          ),
          alignment: Alignment.center,
          child: new Text(
            positiveText??'',
            style: positiveTextStyle??TextStyle(
              color: color_main,
              fontSize: px_16.sp,
            ),
          ),
        ),
      ),
    );
  }
}